package com.fast.alden.admin.web.controller;

import com.fast.alden.admin.entity.dto.ReadMessagesParam;
import com.fast.alden.admin.service.MessageCenterService;
import com.fast.alden.data.model.SysMessage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@Tag(name = "MessageCenterController", description = "消息中心")
@RestController
@RequestMapping("/message-center")
public class MessageCenterController {
    @Resource
    private MessageCenterService messageCenterService;

    @Operation(summary = "获取消息列表")
    @PostMapping("/page")
    public Page<SysMessage> messagePage(@RequestBody @Nullable SysMessage searchVo,
                                        @RequestParam(name = "no", defaultValue = "0") int no,
                                        @RequestParam(name = "size", defaultValue = "10") int size) {
        return messageCenterService.messagePage(searchVo, PageRequest.of(no, size, Sort.by(Sort.Direction.DESC, "createdTime")));
    }

    @Operation(summary = "获取消息详情")
    @GetMapping("/retrieve/{msgId}")
    public SysMessage retrieveMessage(@PathVariable("msgId") Long msgId) {
        return messageCenterService.retrieveMessage(msgId);
    }

    @Operation(summary = "消息设为已读")
    @PostMapping("/read")
    public void readMessages(@RequestBody ReadMessagesParam param) {
        messageCenterService.readMessages(param);
    }
}
